package cn.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @author m  2023/1/27 - 14:26
 * Description:
 */
@Entity
@Getter
@Setter
public class User extends BaseEntity{
    //不映射到数据库
    @Transient
    public static final int TYPE_BANNED = -1; //黑名单
    @Transient
    public static final int TYPE_ADMIN = 0;//管理员
    @Transient
    public static final int TYPE_CERT = 1;//认证
    @Transient
    public static final int TYPE_ORDINARY = 2;//普通用户

    private String nickName;
    private String userName;
    private String phone;
    private String password;
    private String email;
    private byte[] avatar;//头像
    private String salt;//加盐
    private int type;
    private boolean apply;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;//创建
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;//更新
    @Temporal(TemporalType.TIMESTAMP)
    private Date certTime;//认证

    @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private RealName realname;

    @OneToMany(mappedBy = "user",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private List<Article> articles;

    @JsonBackReference
    public List<Article> getArticles() {
        return articles;
    }




    public User(){
        createTime = new Date();
    }

    //重写判断
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        User user = (User) o;
        return id.equals(user.getId());
    }


    public boolean isApply() {
        return apply;
    }

    public void setApply(boolean apply) {
        this.apply = apply;
    }
}
